package com.eastfair.common.controller.micetother;

import cn.hutool.core.date.StopWatch;
import com.eastfair.common.dto.*;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.api.*;
import com.eastfair.venuebooking.vo.FollowRecordVO;
import com.eastfair.venuebooking.api.ServiceProviderFeign;
import core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.*;

/**
 * @ClassNameToCRM
 * @Description TODO
 * @Authoradmin
 * @Date 2023/1/8 15:41
 * @Version 1.0
 **/
@Service
@Slf4j
public class ToCrmSyncTask {

    public static final int LEN = 10;
    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;
    @Resource
    private ExhibitionManageFeign exhibitionManageFeign;
    @Resource
    private ContactFeign contactFeign;
    @Resource
    private CustomerMainFeign customerMainFeign;
    @Resource
    private ServiceProviderFeign serviceProviderFeign;
    @Resource
    private ExhibitionFeign exhibitionFeign;
    @Resource
    private FollowRecordFeign followRecordFeign;


    @Value("${roma.ipport}")
    private String romaIpport;

    @Value("${roma.CRM-X-HW-ID}")
    private String romaXHWID;

    @Value("${roma.CRM-X-HW-APPKEY}")
    private String romaXHWAPPKEY;
    //同步人员
    @Value("${crm.personal.url}")
    private String crmPersonalUrl;
    //同步主办
    @Value("${crm.sponsor.url}")
    private String crmSponsorUrl;
    @Value("${crm.customer.url}")
    private String crmCustomerUrl;
    //同步服务商信息
    @Value("${crm.customer_provider.url}")
    private String crmCustomerProviderUrl;
    //同步组织信息
    @Value("${crm.customer_organization.url}")
    private String crmCustomerOrganizationUrl;
    //同步组织和关联人关系
    @Value("${crm.customer_connected_person.url}")
    private String crmCustomerConnectedPersonUrl;
    //同步项目与主办方关系
    @Value("${crm.customer_object_sponsor.url}")
    private String crmCustomerObjectSponsorUrl;
    //同步项目与服务商关系
    @Value("${crm.customer_service_provider.url}")
    private String crmCustomerServiceProviderUrl;
    //同步项目信息
    @Value("${crm.customer_object.url}")
    private String crmCustomerObjectUrl;
    //同步接触任务
    @Value("${crm.contact_task.url}")
    private String crmContactTaskUrl;
    //同步接触执行信息
    @Value("${crm.contact_exec.url}")
    private String crmContactExecUrl;
    //同步接触轨迹
    @Value("${crm.contact_path.url}")
    private String crmContactPathUrl;
    //同步接触附件
    @Value("${crm.contact_att.url}")
    private String crmContactAttUrl;

    @Value("${common.projectId}")
    private Long commonProjectId;

    /**
     * @Author clm
     * @Description //同步接触任务  同步接触执行信息
     * @Date 9:33 2023/1/9
     * @Param
     * @return
     **/
//    @Scheduled(cron="0/10 * * * * *")
    @XxlJob("syncContactTask")
    public void syncContactTask() {
        ContextUtil.setProjectId(commonProjectId);
        R<List<FollowRecordVO>> listR = followRecordFeign.queryUnsynRecord();
        if (!listR.getIsSuccess() || CollectionUtils.isEmpty(listR.getData())){
            return;
        }
        List<FollowRecordVO> data = listR.getData();
        List<TchTskDto> tchTskDtoList = new ArrayList<>();
        TchTskDtos tchTskDtos = new TchTskDtos();

        List<TskObjfDto> tskObjfDtoList = new ArrayList<>();
        TskObjfDtos tskObjfDtos = new TskObjfDtos();
        for (FollowRecordVO follow : data){
            TchTskDto tchTskDto = new TchTskDto();
            tchTskDto.setTskId(follow.getId() + "");
            tchTskDto.setTskCgy("1");
            tchTskDto.setTskEddt(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tchTskDto.setTskObjTp("11");
            tchTskDto.setTskStdt(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tchTskDto.setTskTopc("无");
            tchTskDto.setCrtEmpId("无");
            tchTskDto.setCrtOrgId("无");
            tchTskDto.setCrtTm(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tchTskDto.setDelInd("0");
            tchTskDto.setDtSrc("mice");
            tchTskDto.setExecMod("1");
            tchTskDto.setLoadBtno(follow.getId() + "");
            tchTskDto.setLoadTm(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tchTskDto.setRltvBsnCode("无");
            tchTskDto.setRltvBsnTp("无");
            tchTskDto.setRltvQstnrInd("无");
            tchTskDto.setRltvQstnrMdlId("无");
            tchTskDto.setScpCntnt("无");
            tchTskDto.setSmy(follow.getContent());
            tchTskDto.setTchMod(follow.getFollowType());
            tchTskDto.setTskCntnt(follow.getContent());
            tchTskDto.setTskCrtWay("1");
            tchTskDto.setTskEddt(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tchTskDto.setTskSt("无");
            tchTskDtoList.add(tchTskDto);


            TskObjfDto tskObjfDto = new TskObjfDto();
            tskObjfDto.setExecEmpId(follow.getFollowUserId() + "");
            tskObjfDto.setTskExecSt("无");
            tskObjfDto.setTskId(follow.getId() + "");
            tskObjfDto.setTskReslTp("2");
            tskObjfDto.setAlctTm(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tskObjfDto.setCrtEmpId(follow.getFollowUserId() + "");
            tskObjfDto.setDelInd("0");
            tskObjfDto.setFuDt(Date.from(follow.getFollowDateTime().atZone(ZoneId.systemDefault()).toInstant()));
            tskObjfDto.setTskObjCode(follow.getId() + "");
            tskObjfDto.setTskObjId(follow.getId() + "");
            tskObjfDto.setTskObjNm("无");
            tskObjfDto.setTskReslDsc("无");
            tskObjfDto.setTskReslTp("2");
            tskObjfDto.setTskSrccd("1");
            tskObjfDto.setUdtEmpId(follow.getId() + "");

            tskObjfDtoList.add(tskObjfDto);
        }

        tchTskDtos.setDtos(tchTskDtoList);
        tskObjfDtos.setDtos(tskObjfDtoList);

        if (!CollectionUtils.isEmpty(tchTskDtoList)){
            String httpUrl = romaIpport + crmContactTaskUrl;
            StopWatch sw = new StopWatch();
            sw.start("syncContactTask");
            log.info("===================================syncContactTask start=======================================");
            commonInterfaceCallRecordService.processHttp(httpUrl, tchTskDtos, null, "1234567890", romaXHWID, romaXHWAPPKEY);
            log.info("===================================syncContactTask end=======================================");
            sw.stop();
            log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        }

        if (!CollectionUtils.isEmpty(tskObjfDtoList)){
            String httpUrl = romaIpport + crmContactExecUrl;
            StopWatch sw = new StopWatch();
            sw.start("syncContactExec");
            log.info("===================================syncContactExec start=======================================");
            commonInterfaceCallRecordService.processHttp(httpUrl, tskObjfDtos, null, "1234567890", romaXHWID, romaXHWAPPKEY);
            log.info("===================================syncContactExec end=======================================");
            sw.stop();
            log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        }


    }


    /**
     * @Author clm
     * @Description //同步接触轨迹  业务不涉及
     * @Date 9:33 2023/1/9
     * @Param
     * @return
     **/
   // @Scheduled(cron="0/10 * * * * *")
    public void syncContactPath() {
        ContextUtil.setProjectId(1869274736816820224L);

        TchTvlDto tchTvlDto = new TchTvlDto();
        tchTvlDto.setCrtEmpId("123456789");
        tchTvlDto.setCrtOrgId("2345678rty");
        List<TchTvlDto> tchTvlDtoList = new ArrayList<>();
        tchTvlDtoList.add(tchTvlDto);
        TchTvlDtos tchTvlDtos = new TchTvlDtos();
        tchTvlDtos.setDtos(tchTvlDtoList);

        if (CollectionUtils.isEmpty(tchTvlDtoList)){
            return;
        }

        String httpUrl = romaIpport + crmContactPathUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncContactPath");
        log.info("===================================syncContactPath start=======================================");
        commonInterfaceCallRecordService.processHttp(httpUrl, tchTvlDtos, null, "1234567890", romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncContactPath end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());

    }


        /**
         * @Author clm
         * @Description //同步接触附件   业务不涉及
         * @Date 9:33 2023/1/9
         * @Param
         * @return
         **/
   // @Scheduled(cron="0/10 * * * * *")
    public void syncContactAtt() {
        ContextUtil.setProjectId(1869274736816820224L);

        TchAtchDto tchAtchDto = new TchAtchDto();
        tchAtchDto.setAtchNameOrig("12345678");
        tchAtchDto.setAtchTp("test 1345678");
        tchAtchDto.setCrtEmpId("ttt34567");
        tchAtchDto.setAtchTp("test gsdef343");
        tchAtchDto.setAtchNameOrig("test 云之城");
        tchAtchDto.setCrtTm(new Date());
        List<TchAtchDto> tchAtchDtoList = new ArrayList<>();
        TchAtchDtos tchAtchDtos = new TchAtchDtos();
        tchAtchDtos.setDtos(tchAtchDtoList);
        if (CollectionUtils.isEmpty(tchAtchDtoList)){
            return;
        }

        String httpUrl = romaIpport + crmContactAttUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncContactAtt");
        log.info("===================================syncContactAtt start=======================================");
        commonInterfaceCallRecordService.processHttp(httpUrl, tchAtchDtoList, null, "1234567890", romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncContactAtt end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());

    }

}
